package api

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"zg3/internal/api/request"
	"zg3/internal/api/response"
	"zg3/internal/models/service"
)

func ListVideo(c *gin.Context) {
	var json request.ListVideo
	if err := c.ShouldBindJSON(&json); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	video := service.ListVideo(&json)

	response.Response(c, 200, "展示成功", video)

}

func BeginVideo(c *gin.Context) {
	var json request.BeginVideo
	if err := c.ShouldBindJSON(&json); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	value, _ := c.Get("userId")
	userId, _ := strconv.Atoi(value.(string))

	video, s := service.BeginVideo(&json, userId)
	if s != "" {
		response.Response(c, 400, s, nil)
		return
	}

	response.Response(c, 200, "视频播放成功", video)

}

func VideoLike(c *gin.Context) {
	var json request.VideoLike
	if err := c.ShouldBindJSON(&json); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	value, _ := c.Get("userId")
	userId, _ := strconv.Atoi(value.(string))

	like, s := service.VideoLike(&json, userId)

	if s != "" {
		response.Response(c, 400, s, nil)
		return
	}

	response.Response(c, 200, "视频点赞成功", like)
}

func FindLikeVideo(c *gin.Context) {

	value, _ := c.Get("userId")
	userId, _ := strconv.Atoi(value.(string))

	video := service.FindLikeVideo(userId)

	response.Response(c, 200, "点赞的视频查询成功", video)

}

func NoLike(c *gin.Context) {
	var json request.NoLike
	if err := c.ShouldBindJSON(&json); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	value, _ := c.Get("userId")
	userId, _ := strconv.Atoi(value.(string))

	like := service.NoLike(&json, userId)

	if like != "" {
		response.Response(c, 400, like, nil)
		return
	}

	response.Response(c, 200, "点赞取消成功", nil)

}

func TopEveryDay(c *gin.Context) {
	service.GetTopVideo()
	video, likes, s := service.FindTopVideo()
	if s != "" {
		response.Response(c, 400, s, nil)
		return
	}

	var map2 = make(map[string]interface{})
	map2["look"] = video
	map2["likes"] = likes

	response.Response(c, 200, "每日top查询成功", map2)
}
